﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft Corp." file="AzureServiceContext.cs">
//   All rights reserved. See License.txt in the project root for license information.
// </copyright>
// 
// --------------------------------------------------------------------------------------------------------------------
namespace AzureManagement
{
    using System;
    using System.Net;

    using RestClient;

    public class AzureServiceContext : RestServiceContext, IAzureServiceContext
    {
        private const string ServiceApiVersionHeader = "x-ms-version";

        private readonly string apiVersion;

        public AzureServiceContext(IServiceCredentials serviceCredentials, Uri serviceUri, string apiVersion)
            : base(serviceUri, serviceCredentials)
        {
            if (string.IsNullOrEmpty(apiVersion))
            {
                throw new ArgumentException("API version must not be null or empty.", "apiVersion");
            }

            this.apiVersion = apiVersion;
        }

        public string SubscriptionId
        {
            get
            {
                var credentials = this.ServiceCredentials as IAzureServiceCredentials;
                return credentials == null ? string.Empty : credentials.SubscriptionId;
            }
        }

        protected override void AddHeaders(WebRequest request)
        {
            request.Headers.Add(ServiceApiVersionHeader, this.apiVersion);
        }
    }
}